package com.inews.business.web;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.inews.base.utils.ExtDataUtil;
import com.inews.base.utils.Identities;
import com.inews.base.utils.Servlets;
import com.inews.business.entity.Brand;
import com.inews.business.service.BrandService;
import com.inews.business.util.BusinessContants;

@Controller
@RequestMapping(value = "/control/brand")
public class BrandController implements ServletContextAware{
	
	@Autowired
	private BrandService brandService;
	
	private ServletContext servletContext;

	@RequestMapping(value = "list")
	public String productTypeList(@RequestParam(value = "sortType", defaultValue = "auto") String sortType,
			@RequestParam(value = "page", defaultValue = "1") int pageNumber, Model model, ServletRequest request) {
		
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
		Page<Brand> brands = brandService.getBrand(searchParams, pageNumber, BusinessContants.PAGE_SIZE, sortType);
		model.addAttribute("resData", ExtDataUtil.genWithPagingList(brands));
//		model.addAttribute("sortType", sortType);
//		model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));
		return "product/brandlist";
	}
	
	
	@RequestMapping(value = "addUI")
	public String addUI() {
		return "product/add_brand";
	}
	
	
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(Brand brand,  @RequestParam("logofile") CommonsMultipartFile logofile, HttpServletRequest request, Model model) throws IOException {
//		System.out.println(name);
		if(!logofile.isEmpty()){
			String contextPath = request.getSession().getServletContext().getRealPath("/images/brand/");
			String imgPath = DateFormatUtils.format(new Date(), "yyyy/MM/dd/HH/mm/");
			String oriImgName = logofile.getOriginalFilename();
			String imgName = Identities.uuid2() + oriImgName.substring(oriImgName.lastIndexOf("."));
			FileUtils.copyInputStreamToFile(logofile.getInputStream(), new File(contextPath + imgPath, imgName));
			System.out.println(contextPath + imgPath);
			brand.setLogopath("/images/brand/"+imgPath+imgName);
		}
		
		brandService.saveBrand(brand);
		model.addAttribute("message", "添加成功");
		model.addAttribute("urladdress", "/control/brand/list");
		return "share/message";
	}


	@Override
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

}
